package com.qn;

import com.tree.TreeUtil;

import java.util.ArrayList;
import java.util.List;

public class TreeIndexer {

    public static void main(String[] args) {
        List<UserTest> roots = new ArrayList<>();

        // 创建多个根级节点
        UserTest root1 = new UserTest(1, 0);
        UserTest root2 = new UserTest(2, 0);
        UserTest root3 = new UserTest(3, 0);

        UserTest child1_1 = new UserTest(4, 1);
        UserTest child1_2 = new UserTest(5, 1);
        UserTest child2_1 = new UserTest(6, 2);
        UserTest child2_2 = new UserTest(7, 2);
        UserTest child3_1 = new UserTest(8, 3);

        roots.add(child1_1);
        roots.add(child1_2);
        roots.add(child2_1);
        roots.add(child2_2);
        roots.add(child3_1);

        roots.add(root1);
        roots.add(root2);
        roots.add(root3);

        List<UserTest> tree = TreeUtil.makeTree(roots, root -> root.getParentId() == 0, (x, y) -> x.getId().equals(y.getParentId()), UserTest::setChildren);

        TreeUtil.setIndexes(tree, UserTest::getChildren);

        // 打印树结构及其序号
        for (UserTest root : tree) {
            printTree(root, 0);
        }
    }

    private static void printTree(UserTest node, int level) {
        if (node == null) {
            return;
        }
        System.out.println(" ".repeat(level * 2) + "Node ID: " + node.getId() + ", Index: " + node.getIndex());
        for (TreeNode<UserTest> child : node.getChildren()) {
            printTree((UserTest) child, level + 1);
        }
    }
}
